{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 第三次练习"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "+ 请务必交到exer3文件夹下，谢绝交到master下\n",
    "+ 请不要改动任何文件，拜托\n",
    "+ 请在10月23日前提交。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "请写一下姓名和学号：\n",
    "+ 姓名  刘婷婷\n",
    "+ 学号  0154590"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 请从read（）函数指针的角度解读下面代码的结果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false,
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "一\n",
      "阵风吹过，龙宫的许多宝物被吹落在建邺城。\n",
      "双寿星日惊叹缘分神奇\n",
      "能够遇到你们\n",
      "就算花光所有运气也在所不惜\n",
      "希望后来的我们依旧快乐\n",
      "掉体脂过八高夜夜嗨唱四零七\n",
      "enmm...\n",
      "嘘~~（撒腿就跑并投掷两颗小心心）\n",
      "一个小时\n",
      "是姐姐姐夫直播吃螃蟹\n",
      "是弟弟直播吃 米果和石榴\n",
      "是我的健胃消食片\n",
      "祝我弟生日快乐 学习快乐 天天快乐\n",
      "（直播完还不是要补作业）\n",
      "你站立在小路的这一端，看着他逐渐消失在小路转弯的地方，而且，他用背影默默告诉你：不必追。\n",
      "\n"
     ]
    }
   ],
   "source": [
    "f = open(\"exer3.txt\",\"r\") #opens file with name of \"test.txt\"\n",
    "print(f.read(1))\n",
    "print(f.read())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**解读请点开此cell后，写下面：**\n",
    "\n",
    "f = open(\"exer3.txt\",\"r\")#将文件“exer3.text”的内容放到字符串变量f中\n",
    "print(f.read(1))#用read（）读取字符串变量f中的1个字节的内容，并输出\n",
    "print(f.read())#反复调用read（），直到读完所有内容\n",
    " \n",
    " \n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 请从readline（）函数指针的角度解读下面代码的结果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "一阵风吹过，龙宫的许多宝物被吹落在建邺城。\n",
      "\n",
      "双寿星日惊叹缘分神奇\n",
      "\n"
     ]
    }
   ],
   "source": [
    "f = open(\"exer3.txt\",\"r\") #opens file with name of \"test.txt\"\n",
    "print(f.readline())\n",
    "print(f.readline())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**解读请点开此cell后，写下面：**\n",
    " \n",
    "f = open(\"exer3.txt\",\"r\") #将文件“exer3.text”的内容放到字符串变量f中\n",
    "print(f.readline())#调用readline（）读取字符串变量f的第一行内容，并输出\n",
    "print(f.readline())#继续调用readline（）读取字符串变量f的下一行内容，并输出\n",
    " \n",
    " \n",
    " \n",
    " \n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 请将上面的代码扩充为循环，直至显示文本全部内容。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "一阵风吹过，龙宫的许多宝物被吹落在建邺城。\n",
      "\n",
      "双寿星日惊叹缘分神奇\n",
      "\n",
      "能够遇到你们\n",
      "\n",
      "就算花光所有运气也在所不惜\n",
      "\n",
      "希望后来的我们依旧快乐\n",
      "\n",
      "掉体脂过八高夜夜嗨唱四零七\n",
      "\n",
      "enmm...\n",
      "\n",
      "嘘~~（撒腿就跑并投掷两颗小心心）\n",
      "\n",
      "一个小时\n",
      "\n",
      "是姐姐姐夫直播吃螃蟹\n",
      "\n",
      "是弟弟直播吃 米果和石榴\n",
      "\n",
      "是我的健胃消食片\n",
      "\n",
      "祝我弟生日快乐 学习快乐 天天快乐\n",
      "\n",
      "（直播完还不是要补作业）\n",
      "\n",
      "你站立在小路的这一端，看着他逐渐消失在小路转弯的地方，而且，他用背影默默告诉你：不必追。\n",
      "\n",
      "20几岁的时候认为一只爱马仕里面可以装下辉煌人生，30好几了发现真正活出自我，拎不拎爱马仕没多大关糸，完美的人生不是要拥有一切……从容淡定活出自己更重要……安！我的全世界……\n",
      "\n",
      "深感数学基础没打好。最简单统计啊…这些结果为啥都如此陌生，理解不能…半夜三更脑壳疼（图文无关，这些是体现数学之美的\n",
      "\n",
      "我的朋友圈封面每天都被我60多岁的舅舅霸占快递小哥都跑不过他\n",
      "\n",
      "舞动体验课上，当老师播放《钟》时我情不自禁模拟弹琴开始手指的舞蹈～14年前能弹的曲子，现在只能比划一下了……人生果真是有得有失\n",
      "\n",
      "胖了胖了\n",
      "\n",
      "需要割肉\n",
      "\n",
      "和这个女人一起要横向发展了\n"
     ]
    }
   ],
   "source": [
    "x = 1\n",
    "with open(\"exer3.txt\",\"r\") as f:\n",
    "    lines = f.readline()  \n",
    "    while lines:\n",
    "        print(lines)\n",
    "        x = x + 1\n",
    "        lines = f.readline()\n",
    "        "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 下面的代码实现了把exer3的内容放到myList里，但是里面出现了很多换行符，请修改下代码使得myList里的内容没有换行符"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['一阵风吹过，龙宫的许多宝物被吹落在建邺城。\\n', '双寿星日惊叹缘分神奇\\n', '能够遇到你们\\n', '就算花光所有运气也在所不惜\\n', '希望后来的我们依旧快乐\\n', '掉体脂过八高夜夜嗨唱四零七\\n', 'enmm...\\n', '嘘~~（撒腿就跑并投掷两颗小心心）\\n', '一个小时\\n', '是姐姐姐夫直播吃螃蟹\\n', '是弟弟直播吃 米果和石榴\\n', '是我的健胃消食片\\n', '祝我弟生日快乐 学习快乐 天天快乐\\n', '（直播完还不是要补作业）\\n', '你站立在小路的这一端，看着他逐渐消失在小路转弯的地方，而且，他用背影默默告诉你：不必追。\\n']\n"
     ]
    }
   ],
   "source": [
    "f = open(\"exer3.txt\",\"r\") #opens file with name of \"test.txt\"\n",
    "myList = []\n",
    "for line in f:\n",
    "    myList.append(line)\n",
    "print(myList)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['一阵风吹过，龙宫的许多宝物被吹落在建邺城。', '双寿星日惊叹缘分神奇', '能够遇到你们', '就算花光所有运气也在所不惜', '希望后来的我们依旧快乐', '掉体脂过八高夜夜嗨唱四零七', 'enmm...', '嘘~~（撒腿就跑并投掷两颗小心心）', '一个小时', '是姐姐姐夫直播吃螃蟹', '是弟弟直播吃 米果和石榴', '是我的健胃消食片', '祝我弟生日快乐 学习快乐 天天快乐', '（直播完还不是要补作业）', '你站立在小路的这一端，看着他逐渐消失在小路转弯的地方，而且，他用背影默默告诉你：不必追。', '20几岁的时候认为一只爱马仕里面可以装下辉煌人生，30好几了发现真正活出自我，拎不拎爱马仕没多大关糸，完美的人生不是要拥有一切……从容淡定活出自己更重要……安！我的全世界……', '深感数学基础没打好。最简单统计啊…这些结果为啥都如此陌生，理解不能…半夜三更脑壳疼（图文无关，这些是体现数学之美的', '我的朋友圈封面每天都被我60多岁的舅舅霸占快递小哥都跑不过他', '舞动体验课上，当老师播放《钟》时我情不自禁模拟弹琴开始手指的舞蹈～14年前能弹的曲子，现在只能比划一下了……人生果真是有得有失', '胖了胖了', '需要割肉', '和这个女人一起要横向发展了']\n"
     ]
    }
   ],
   "source": [
    "f = open(\"exer3.txt\",\"r\") \n",
    "myList = []\n",
    "for line in f:\n",
    "    myList.append(line.rstrip())\n",
    "print(myList)\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.0"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
